package com.example.feifei.view.popwindow;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.feifei.newproject.R;

/**
 * 自写百分比进度条
 * Created by 陈飞飞 on 2018\7\19 0019.
 */

public class ProgressNumber extends View {
    //左侧画笔
    Paint leftPaint = new Paint();
    //右侧画笔
    Paint rightPaint = new Paint();
    //百分比画笔
    Paint textPaint = new Paint();
    //百分比进度条
    private int progressInt = 0;
    //组件宽度
    private int viewWidth;
    //组件高度
    private int viewHeight;
    //高度中心
    private int viewYCenter;
    //包裹文字的矩形
    private Rect textRect = new Rect();
    private int textWidth;
    //文字底部Y坐标
    private int textBottomY;


    private Context context;

    public ProgressNumber(Context context) {
        super(context);
        this.context = context;
        initData();
    }

    public ProgressNumber(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initData();
    }


    //初始化画笔属性
    private void initData() {
        leftPaint.setColor(getResources().getColor(R.color.blue_text));
        leftPaint.setStrokeWidth(10);
        leftPaint.setTypeface(Typeface.DEFAULT);

        rightPaint.setColor(getResources().getColor(R.color.gray_bg));
        rightPaint.setStrokeWidth(10);
        rightPaint.setTypeface(Typeface.DEFAULT);

        textPaint.setColor(getResources().getColor(R.color.blue_text));
        textPaint.setStrokeWidth(10);
        textPaint.setTextSize(sp2px(context,15));
        textPaint.setTypeface(Typeface.DEFAULT);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        textPaint.getTextBounds("100%",0,"100%".length(),textRect);
        textWidth = textRect.width();
        textBottomY =textRect.height()/2;

        viewWidth = getMeasuredWidth();
        viewHeight = getMeasuredHeight();
        viewYCenter = viewHeight/2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float progressFloat = progressInt / 100.0f;

        canvas.drawLine(0,viewYCenter,(viewWidth-textWidth)*progressFloat,viewYCenter,leftPaint);

        if (progressInt < 10) {
            canvas.drawLine((viewWidth-textWidth)*progressFloat+textWidth* 0.5f,viewYCenter,viewWidth,viewYCenter,rightPaint);
        } else if (progressInt < 100) {
            canvas.drawLine((viewWidth-textWidth)*progressFloat+textWidth* 0.75f,viewYCenter,viewWidth,viewYCenter,rightPaint);
        } else {
            canvas.drawLine((viewWidth-textWidth)*progressFloat+textWidth,viewYCenter,viewWidth,viewYCenter,rightPaint);
        }

        canvas.drawText(progressInt+"%",(viewWidth-textWidth)*progressFloat,viewYCenter+textBottomY,textPaint);
    }
    //刷新进度条进度
    public void setProgressInt(int progress){
        this.progressInt = progress;
        invalidate();
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     */
    private static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     */
    private static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}
